package com.stox.services.data.downloader.model;

import java.text.ParseException;
import java.util.Date;

import com.stox.shared.core.model.intf.Identifiable;
import com.stox.shared.core.util.Constant;
import com.stox.shared.core.util.StringUtil;

public class ExchangeDownloadMetadata implements Identifiable{

	private Long exchangeId;
	private Date lastDownloadDate;
	
	public ExchangeDownloadMetadata() {
		
	}
	
	public ExchangeDownloadMetadata(String text) {
		String[] tokens = text.split(",");
		if(0 < tokens.length && StringUtil.hasText(tokens[0])) exchangeId = Long.parseLong(tokens[0]);
		try {
			if(1 < tokens.length && StringUtil.hasText(tokens[1])) lastDownloadDate = Constant.dateFormat.parse(tokens[1]);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public Long getId() {
		return getExchangeId();
	}
	
	@Override
	public void setId(Long id) {
		setExchangeId(id);
	}
	
	public Long getExchangeId() {
		return exchangeId;
	}
	
	public void setExchangeId(Long exchangeId) {
		this.exchangeId = exchangeId;
	}
	
	public Date getLastDownloadDate() {
		return lastDownloadDate;
	}
	
	public void setLastDownloadDate(Date lastDownloadDate) {
		this.lastDownloadDate = lastDownloadDate;
	}
	
	@Override
	public String toString() {
		return exchangeId+","+ (null == lastDownloadDate ? "" : Constant.dateFormat.format(lastDownloadDate));
	}
	
	@Override
	public boolean equals(Object obj) {
		if(null == obj || !(obj instanceof ExchangeDownloadMetadata)){
			return false;
		}
		ExchangeDownloadMetadata other = (ExchangeDownloadMetadata)obj;
		if(null == exchangeId || null == other.getExchangeId()){
			return false;
		}
		return exchangeId.equals(other.getExchangeId());
	}

}
